{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "998c80fd",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-07-10T06:24:42.173758Z",
     "start_time": "2022-07-10T06:24:42.029779Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.cd122e9d12f18afe90c0456dd8d2db44.315360000.1972794291.282335-25904774', 'expires_in': 2592000, 'session_key': '9mzdA5uL0G3wTcgTryKTEUje8QGMoRjQrWpP396o6bjp030crIwuWeVP+y24w5VFNM4ZKfUSACTr0/1P7bBtht1LxVr5vg==', 'access_token': '24.81b569e2467c06133b558a7fabb25977.2592000.1660026291.282335-25904774', 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx', 'session_secret': 'c236241063ed2e9f83b89a78215f855a'}\n"
     ]
    }
   ],
   "source": [
    "import requests \n",
    "\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=YLXeG09mXsjyv2Vx8HnQv9O3&client_secret=rmYbtN7n02IYyEIoSuG13G34DAXrqg8C'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a4f6d4ac",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-07-10T06:24:42.999979Z",
     "start_time": "2022-07-10T06:24:42.991975Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.81b569e2467c06133b558a7fabb25977.2592000.1660026291.282335-25904774'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "access_token = response.json()['access_token']\n",
    "access_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ce4a75aa",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-07-10T06:41:18.818263Z",
     "start_time": "2022-07-10T06:41:18.576844Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'name': '酸菜鱼', 'calorie': '98', 'probability': '0.943978', 'has_calorie': True}, {'name': '水煮鱼', 'calorie': '153', 'probability': '0.0258497', 'has_calorie': True}, {'name': '酸汤鱼', 'calorie': '119', 'probability': '0.016182', 'has_calorie': True}, {'name': '鱼片', 'calorie': '303', 'probability': '0.00337536', 'has_calorie': True}, {'name': '黑鱼', 'calorie': '85', 'probability': '0.00128102', 'has_calorie': True}], 'log_id': 1546021767577006288}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "菜品识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/dish\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('fish.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img,\"top_num\":5}\n",
    "access_token = '24.81b569e2467c06133b558a7fabb25977.2592000.1660026291.282335-25904774'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "4e249954",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-07-10T06:42:03.702077Z",
     "start_time": "2022-07-10T06:42:03.693879Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "a7d09ae4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-07-10T06:42:32.185610Z",
     "start_time": "2022-07-10T06:42:32.170612Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>result_num</th>\n",
       "      <th>result</th>\n",
       "      <th>log_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5</td>\n",
       "      <td>[{'name': '酸菜鱼', 'calorie': '98', 'probability...</td>\n",
       "      <td>1546021767577006288</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   result_num                                             result  \\\n",
       "0           5  [{'name': '酸菜鱼', 'calorie': '98', 'probability...   \n",
       "\n",
       "                log_id  \n",
       "0  1546021767577006288  "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.json_normalize(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61643902",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "4a8feddf",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-07-10T06:58:37.335527Z",
     "start_time": "2022-07-10T06:58:37.222586Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{\"code\":200,\"msg\":\"success\",\"newslist\":[{\"name\":\"鸡蛋(均值)\",\"rl\":144,\"las\":0.11,\"gai\":56,\"dbz\":13.3,\"su\":0.27,\"mei\":10,\"zf\":8.8,\"ys\":0.2,\"tei\":2,\"shhf\":2.8,\"wsfc\":0,\"meng\":0.04,\"ssxw\":0,\"wsse\":1.84,\"xin\":1.1,\"wssa\":234,\"dgc\":0,\"tong\":0.15,\"lb\":1,\"jia\":154,\"ling\":130,\"shc\":74.1,\"la\":131.5,\"xi\":14.34,\"type\":\"蛋类\"},{\"name\":\"鸡蛋(白皮)\",\"rl\":138,\"las\":0.09,\"gai\":48,\"dbz\":12.7,\"su\":0.31,\"mei\":14,\"zf\":9,\"ys\":0.2,\"tei\":2,\"shhf\":1.5,\"wsfc\":0,\"meng\":0.03,\"ssxw\":0,\"wsse\":1.23,\"xin\":1,\"wssa\":310,\"dgc\":585,\"tong\":0.06,\"lb\":1,\"jia\":98,\"ling\":176,\"shc\":75.8,\"la\":94.7,\"xi\":16.55,\"type\":\"蛋类\"},{\"name\":\"鸡蛋(红皮)\",\"rl\":156,\"las\":0.13,\"gai\":44,\"dbz\":12.8,\"su\":0.32,\"mei\":11,\"zf\":11.1,\"ys\":0.2,\"tei\":2.3,\"shhf\":1.3,\"wsfc\":0,\"meng\":0.04,\"ssxw\":0,\"wsse\":2.29,\"xin\":1.01,\"wssa\":194,\"dgc\":585,\"tong\":0.07,\"lb\":1,\"jia\":121,\"ling\":182,\"shc\":73.8,\"la\":125.7,\"xi\":14.98,\"type\":\"蛋类\"},{\"name\":\"鸡蛋(土鸡)\",\"rl\":138,\"las\":0.12,\"gai\":76,\"dbz\":14.4,\"su\":0.19,\"mei\":5,\"zf\":6.4,\"ys\":0,\"tei\":1.7,\"shhf\":5.6,\"wsfc\":0,\"meng\":0.06,\"ssxw\":0,\"wsse\":1.36,\"xin\":1.28,\"wssa\":199,\"dgc\":1338,\"tong\":0.32,\"lb\":1,\"jia\":244,\"ling\":33,\"shc\":72.6,\"la\":174,\"xi\":11.5,\"type\":\"蛋类\"},{\"name\":\"鸡蛋白\",\"rl\":60,\"las\":0.04,\"gai\":9,\"dbz\":11.6,\"su\":0.31,\"mei\":15,\"zf\":0.1,\"ys\":0.2,\"tei\":1.6,\"shhf\":3.1,\"wsfc\":0,\"meng\":0.02,\"ssxw\":0,\"wsse\":0.01,\"xin\":0.02,\"wssa\":0,\"dgc\":0,\"tong\":0.05,\"lb\":0.8,\"jia\":132,\"ling\":18,\"shc\":84.4,\"la\":79.4,\"xi\":6.97,\"type\":\"蛋类\"},{\"name\":\"鸡蛋白(乌骨鸡)\",\"rl\":44,\"las\":0,\"gai\":9,\"dbz\":9.8,\"su\":0.31,\"mei\":10,\"zf\":0.1,\"ys\":0.1,\"tei\":0,\"shhf\":1,\"wsfc\":0,\"meng\":0.01,\"ssxw\":0,\"wsse\":0,\"xin\":0.01,\"wssa\":0,\"dgc\":0,\"tong\":0.01,\"lb\":0.7,\"jia\":109,\"ling\":17,\"shc\":88.4,\"la\":165.1,\"xi\":2.99,\"type\":\"蛋类\"},{\"name\":\"鸡蛋黄\",\"rl\":328,\"las\":0.33,\"gai\":112,\"dbz\":15.2,\"su\":0.29,\"mei\":41,\"zf\":28.2,\"ys\":0.1,\"tei\":6.5,\"shhf\":3.4,\"wsfc\":0,\"meng\":0.06,\"ssxw\":0,\"wsse\":5.06,\"xin\":3.79,\"wssa\":438,\"dgc\":1510,\"tong\":0.28,\"lb\":1.7,\"jia\":95,\"ling\":240,\"shc\":51.5,\"la\":54.9,\"xi\":27.01,\"type\":\"蛋类\"},{\"name\":\"鸡蛋黄(乌骨鸡)\",\"rl\":263,\"las\":0.07,\"gai\":107,\"dbz\":15.2,\"su\":0.36,\"mei\":16,\"zf\":19.9,\"ys\":0.1,\"tei\":0.5,\"shhf\":5.7,\"wsfc\":0,\"meng\":0.04,\"ssxw\":0,\"wsse\":7.64,\"xin\":3.1,\"wssa\":179,\"dgc\":2057,\"tong\":0.7,\"lb\":1.4,\"jia\":105,\"ling\":216,\"shc\":57.8,\"la\":57.2,\"xi\":22.62,\"type\":\"蛋类\"},{\"name\":\"鸡蛋粉[全蛋粉]\",\"rl\":545,\"las\":0.05,\"gai\":954,\"dbz\":43.4,\"su\":0.4,\"mei\":46,\"zf\":36.2,\"ys\":0,\"tei\":10.5,\"shhf\":11.3,\"wsfc\":0,\"meng\":0.22,\"ssxw\":0,\"wsse\":11.56,\"xin\":5.95,\"wssa\":525,\"dgc\":2251,\"tong\":0.28,\"lb\":6.6,\"jia\":357,\"ling\":780,\"shc\":2.5,\"la\":393.2,\"xi\":39.1,\"type\":\"蛋类\"},{\"name\":\"鸡蛋黄粉\",\"rl\":644,\"las\":0,\"gai\":266,\"dbz\":31.6,\"su\":0.25,\"mei\":22,\"zf\":55.1,\"ys\":0,\"tei\":10.6,\"shhf\":5.3,\"wsfc\":0,\"meng\":0.05,\"ssxw\":0,\"wsse\":14.43,\"xin\":6.66,\"wssa\":776,\"dgc\":2850,\"tong\":0.1,\"lb\":3.4,\"jia\":103,\"ling\":905,\"shc\":4.6,\"la\":89.8,\"xi\":27.7,\"type\":\"蛋类\"}]}'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import urllib.parse\n",
    "import sys, urllib, json\n",
    "word='鸡蛋'\n",
    "word=urllib.parse.quote(word)\n",
    "url = 'http://api.tianapi.com/txapi/nutrient/index?key=7d86e33baddf379fba0f42a561d6f86e&mode=0&word=%s'%word\n",
    "urll=url\n",
    "req = urllib.request.Request(urll)\n",
    "resp = urllib.request.urlopen(req)\n",
    "content = resp.read().decode('utf-8')\n",
    "content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "601c5784",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
